#coding:utf8
import cherrypy
import datetime
from ConnectMSSQL import ms

from head import print_head
from end import print_end

from utf8code import *


_js = """
        <script type="text/javascript">
            $(function() 
            {
                $("#backbut").click(function()
                {
                    exit();
                });
                
                //建档
                $("#newbut").click(function()
                {
                    window.open("/sbxz");
                });               

                //变更记录维护
                $("#bgbut").click(function()
                {
                    window.open("/sbbgxx");
                });             

                //变更记录查询
                $("#bgserchbut").click(function()
                {
                    window.open("/sbbgjl");
                });
            });
        </script>
        """

_top = """
        <div id="div_operate">
            <input type="button" id="newbut" value="建档" />
            <input type="button" id="bgbut" value="变更记录维护" />
            <input type="button" id="bgserchbut" value="变更记录查询" />
            <input type="button" id="exitbut" onclick="exit()" value="关闭" />
        </div>
    <form method="get" action="updateSB">
      <div id="div_operate">
        <table width="auto">
          <tr>
            <td class="class_td">设备编号</td>
            <td><font>%s</font></td>
            <td class="class_td">固定资产编号</td>
            <td><input type="text" name="gdzcbhtext" value="%s" /></td>
            <td class="class_td">购买日期<font>*</font></td>
            <td><input class="Wdate" name="gmrqtext" id="gmrqtext" type="text" value="%s" onClick="WdatePicker()"></td>
          </tr>
          <tr>
            <td class="class_td">设备类型<font>*</font></td>
            <td><select name="sblxselt" id="sblxselt"><option></option>%s</select></td>
            <td class="class_td">品牌及型号<font>*</font></td>
            <td><input type="text" name="ppxhtext" value="%s" /></td>
            <td class="class_td">设备名称(S/N)</td>
            <td><input type="text" name="sbmctext" value="%s" /></td>
          </tr>
          <tr>
            <td class="class_td">基本配置</td>
            <td colspan="5"><input type="text" name="jbpztext" value="%s" /></td>
          </tr>
          <tr>
            <td class="class_td">网络配置<font>MAC,IP</font></td>
            <td colspan="5"><input type="text" name="wlpztext" value="%s" /></td>
          </tr>
          <tr>
            <td class="class_td">随机附件</td>
            <td colspan="5"><input type="text" name="sjfjtext" value="%s" /></td>
          </tr>
          <tr>
            <td class="class_td" colspan="6">备注</td>
          </tr>
          <tr>
            <td colspan="6"><textarea name="bztextarea">%s</textarea></td>
          </tr>
        </table>
    </div>
      <div align="right">
        <input type="submit" id="savebut" value="更新" />
        <input type="button" id="backbut" value="取消" />
        <input type="reset" id="resetbut" value="重置" />
      </div>
        """
           
_end = """
    </form>
"""



class SBWH:
    sbbh = ''
    @cherrypy.expose
    def index(self,sbbh):
        #判断设备编号是否为空    
        if sbbh:
            self.sbbh = sbbh
        else:
            return
        
        _mid_init_sblx = """
                <option>%s</option>
                """
                
        _mid_sblx = ''
        
        sql_sbxx = """
            SELECT SBBH
                ,GDZCBH
                ,GMRQ
                ,SBLX
                ,PPXH
                ,SBMC
                ,JBPZ
                ,WLPZ
                ,SJFJ
                ,BZ
            FROM T_SB
            WHERE SBBH = '%s'
        """        
        dataList_1 = ms.ExecQuery(sql_sbxx % sbbh)
        _mid_1 = ''
        
        sql_sblx = "SELECT DISTINCT SBLX FROM T_SB"#维护的类型取得是'已有的设备类型'
        sql_sblx = "SELECT SBLX FROM T_SBLX ORDER BY SBLX"#维护的类型取得是'所有设备类型'
        dl_sblx = ms.ExecQuery(sql_sblx)
        for sbxx in dataList_1:
            #和数据库中的所选类型做比较，并默认设备信息中的类型为默认选中，最后一起加载到页面中
            for sblx in dl_sblx:
                if sblx[0] != sbxx[3]:
                    _mid_sblx += _mid_init_sblx % sblx
                else:
                    _mid_sblx += '<option selected="selected">%s</option>' % sblx
            _mid_1 = _top % (sbxx[0],sbxx[1],datetime.datetime.strftime(sbxx[2],'%Y-%m-%d'),_mid_sblx,sbxx[4],sbxx[5],sbxx[6],sbxx[7],sbxx[8],sbxx[9])

        return utf8code(print_head % ('设备维护','设备维护') + _js + _mid_1 + _end + print_end)
    
    #////////////////////////////////////////////////////////////
    #更新设备信息，前台只负责与后台交互，显示到页面上；尽量将后台逻辑封装在存储过程中
    #////////////////////////////////////////////////////////////
    @cherrypy.expose
    def updateSB(self,gdzcbhtext,gmrqtext,sblxselt,ppxhtext,sbmctext,jbpztext,wlpztext,sjfjtext,bztextarea):
            if (len(sblxselt) > 0) and (len(gmrqtext) > 0) and (len(ppxhtext) > 0):
                if len(wlpztext.strip()) > 16:
                    sql_check = "SELECT COUNT(wlpz) FROM T_SB WHERE wlpz = '%s'" % wlpztext[:16]
                    if ms.ExecQuery(sql_check)[0][0] > 1:#查出数据则表示已存在相同MAC的其它设备,将不允许插入,结果不会为NULL，查不到返回的是数字0
                        return utf8code(print_head % ("操作提示","更新失败!-_-||") + """<body onLoad="errorBack()">已存在<font>相同MAC地址</font>的设备,请查找并更新资料!正在返回上一页,请稍等......</body>""" + print_end)
                sql = "EXECUTE PRC_UPDATE_SB '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s';" % (self.sbbh,gdzcbhtext,gmrqtext,sblxselt,ppxhtext,sbmctext,jbpztext,wlpztext,sjfjtext,bztextarea)
                ms.ExecNonQuery(sql)
                return print_head % ("操作提示","更新成功!*^_^*") + "<body onLoad=" + """javascript:setTimeout('exit()',3000);""" + ">正在关闭此页,请稍等......</body>" + print_end
            else:
                return print_head % ("操作提示","更新失败!-_-||") + """<body onLoad="errorBack()">带红色<font>*</font>号的为必填项,正在返回上一页,请稍等......</body>""" + print_end
